package com.finals.function.view;

import android.accessibilityservice.AccessibilityService;
import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.finals.function.R;
import com.finals.function.app.BaseApplication;
import com.finals.function.service.NotificationAccessibilityService;
import com.finals.function.util.OnScreenCastCallback;
import com.finals.function.util.Util;

public class TouchBaseView extends TouchCircle implements View.OnLongClickListener, OnScreenCastCallback {

	Context context;

	BaseApplication mApplication;

	WindowManager mWindowManager;

	WindowManager.LayoutParams mParams;

	NotificationAccessibilityService mAccessibilityService;

	public TouchBaseView(Context context) {
		super(context);
		InitWindow(context);
	}

	public TouchBaseView(Context context, AttributeSet attrs) {
		super(context, attrs);
		InitWindow(context);
	}

	@TargetApi(19)
	private void InitWindow(Context context) {
		this.context = context;
		if (!isInEditMode()) {
			this.mApplication = (BaseApplication) context.getApplicationContext();
		}
		int size = getResources().getDimensionPixelSize(R.dimen.touch_size);
		mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		mParams = new WindowManager.LayoutParams();
		mParams.type = Util.getWindowType(mApplication.getBaseConfig().getWindowMode());
		mParams.format = PixelFormat.RGBA_8888;
		mParams.gravity = Gravity.LEFT | Gravity.TOP;
		mParams.width = size;
		mParams.height = size;
		mParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		mParams.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
		mParams.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
		DisplayMetrics outMetrics = new DisplayMetrics();
		mWindowManager.getDefaultDisplay().getMetrics(outMetrics);
		mParams.y = outMetrics.heightPixels - context.getResources().getDimensionPixelSize(R.dimen.touch_bottom);
		mParams.x = -20;

		this.setOnLongClickListener(this);
	}

	boolean isEidt = false;

	@Override
	public boolean onLongClick(View view) {
		isEidt = true;
		circleX = 0;
		circleY = 0;
		postInvalidate();
		return true;
	}

	public void setAccessibilityService(NotificationAccessibilityService service) {
		this.mAccessibilityService = service;
	}

	public void show() {
		dismiss();
		try {
			mWindowManager.addView(this, mParams);
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(context, "添加失败", Toast.LENGTH_SHORT).show();
		}
	}

	public void dismiss() {
		if (this.getParent() != null) {
			mWindowManager.removeView(this);
		}
	}

	private void UpdateLocation(float x, float y) {
		mParams.x = (int) x;
		mParams.y = (int) y;
		mWindowManager.updateViewLayout(this, mParams);
	}

	float xInWindow, yInWindow;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (isEidt) {
			android.util.Log.e("Finals", "长按编辑模式" + event.getAction());
			switch (event.getAction()) {
			case MotionEvent.ACTION_CANCEL:
			case MotionEvent.ACTION_UP:
				isEidt = false;
				break;
			case MotionEvent.ACTION_MOVE:
				xInWindow = event.getRawX();
				yInWindow = event.getRawY() - Util.getStatusBarHeight(context);
				UpdateLocation(xInWindow - XinView, yInWindow - YinView);
				break;
			default:
				break;
			}
			return true;
		} else {
			android.util.Log.e("Finals", "正常模式 " + event.getAction());
			return super.onTouchEvent(event);
		}
	}

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	public void Goback() {
		if (mAccessibilityService != null) {
			mAccessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
		} else {
			android.util.Log.e("Finals", "无法返回");
		}
	}

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	public void GoHome() {
		if (mAccessibilityService != null) {
			mAccessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);
		} else {
			android.util.Log.e("Finals", "调用返回桌面失败");
		}
	}

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	public void GoRecent() {
		if (mAccessibilityService != null) {
			mAccessibilityService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);
		} else {
			android.util.Log.e("Finals", "调用打开最近任务失败");
		}
	}

	@Override
	public void OpenFunction(int type) {
		if (type == 0) {
			Goback();
		} else if (type == 1) {
			GoHome();
		} else if (type == 2) {
			GoRecent();
		} else if (type == 3) {
			if (mApplication.isSupportScreenShot()) {
				ResetLocation();
				this.setVisibility(View.INVISIBLE);
				mApplication.StartShotCast(this);
			}
		} else if (type == 4) {
			mApplication.ChangeSone(2);
		} else if (type == 5) {
			OpenRecent();
		} else if (type == 6) {
			Log.e("Finals", "切换完毕");
			mApplication.ChangeSone(1);
		}
	}

	private void OpenRecent() {
		if (mAccessibilityService != null) {
			mAccessibilityService.OpenRecent();
		}
	}

	@Override
	public void onScreenCastFinish() {
		this.setVisibility(View.VISIBLE);

		Intent intent = new Intent();
		PendingIntent mIntent = PendingIntent.getActivity(mApplication, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		String titleString = getResources().getString(R.string.app_name);
		mApplication.showNotification(titleString, "截图完毕", mIntent, 1001);
	}

	@Override
	public void onScreenCastStart() {

	}

	public void UpdateMode() {
		mParams.type = Util.getWindowType(mApplication.getBaseConfig().getWindowMode());
		show();
	}
}
